<?php

namespace App\Http\Controllers\Home;


use App\Http\Model\Home\User;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redirect;


class LoginController extends Controller
{

    /**
     *          登录
     *
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function login_in(Request $request)
    {
        //      get 请求
        if ($request->isMethod('get'))
        {
            return view('Home/Login/login_in');
        }

        //      post请求
        if ($request->isMethod('post'))
        {
            $username = $request->input('username');
            $passwd   = $request->input('passwd');


            //      如果手机登录
            preg_match('/^1[3,5,7,8]\d{9}$/',$username,$match);

            if ($match)
            {
                $res = $this->check_pass($username,$passwd,'mobile');

            }

            //       如果用户名登录
                $res = $this->check_pass($username,$passwd,'user_name');

            return $res;
        }
    }


    /**
     *  验证密码将信息写入session
     *
     * @param $user 用户名
     * @param $pass 密码
     * @param $type 类型
     * @return \Illuminate\Http\JsonResponse
     */
    protected function check_pass($user,$pass,$type)
    {

        $userModel = new User();
        $userInfo =$userModel->where($type, $user)->first();

        if(empty($userInfo))
        {
            return response()->json(['info'=>'该用户不存在，请注册','status'=>0]);
        }

        if (password_verify($pass,$userInfo['password']))
        {
            unset($userInfo['password']);
            session(['sign_user_info'=>$userInfo]);

            return response()->json(['info'=>'登陆成功','status'=>1]);

        }else{
            return response()->json(['info'=>'用户名或者密码错误','status'=>$userInfo]);
        }

    }

    /**
     *          用户注册
     *
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function register(Request $request)
    {
        // get请求
        if ($request->isMethod('get'))
        {
            return view('Home/Login/register');
        }

        //  post 请求

        if ($request->isMethod('post'))
        {
            $data['username'] = $request->input('username');
            $data['passwd'] = $request->input('passwd');
            $repass = $request->input('repasswd');
            $data['mobile'] = $request->input('mobile');
            $phonecode = $request->input('phonecode');

            //      用户名验证

            if (empty($data['username']))
            {
                return response()->json(['info'=>'用户名称为空','status'=>0]);
            }
            if (strlen($data['username']) < 2  || strlen($data['username']) > 16)
            {
                return response()->json(['info'=>'用户名称长度应在2~16为之间','status'=>strlen($data['username'])]);
            }

            //      密码验证

            if (empty($data['passwd']))
            {
                return response()->json(['info'=>'密码为空','status'=>0]);
            }

            if ($data['passwd']  !=  $repass)
            {
                return response()->json(['info'=>'两次输入的密码不一致','status'=>0]);
            }

            //      手机号验证

            preg_match('/^1[3,5,7,8]\d{9}$/',$data['mobile'],$res);
            if (!$res)
            {
                return response()->json(['info'=>'您输入的手机号有误','status'=>0]);
            }


            //      验证码验证

            if (empty(session('captcha')))
            {
                return response()->json(['info'=>'请获取验证码','status'=>0]);
            }
            if ($phonecode  != session('captcha'))
            {
                return response()->json(['info'=>'验证码错误','status'=>0]);
            }

            //      验证重复性

            $user = new User();
            $us =$user->where('user_name', $data['username'])->first();

            if ($us)
            {
                return response()->json(['info'=>'该用户名已经被使用','status'=>0]);
            }

            $mo = $user->where('mobile', $data['mobile'])->first();
            if ($mo)
            {
                return response()->json(['info'=>'该手机已经被使用','status'=>0]);
            }


            //      添加到数据库

            $user->user_name = $data['username'];
            $user->password  = password_hash($data['passwd'],PASSWORD_DEFAULT);
            $user->mobile    = $data['mobile'];

            $result = $user->save();

            if ($result)
            {
                session('bz_user_info',$data);
                return response()->json(['info'=>'OK','status'=>1]);
            }else{
                return response()->json(['info'=>'失败','status'=>0]);
            }

        }

    }


    /**
     * 获取验证码
     *
     * @param Request $request
     * @return mixed
     */
    public function get_code(Request $request)
    {
        //
        if ($request->isMethod('post'))
        {

            $mother = '2346789abcdefghjmnpqrtuxyzABCDEFGHJMNPQRTUXYZ';
            $str = '';
            for ($i=0;$i<4;$i++)
            {
                $str .= $mother{rand(0,(strlen($mother)-1))};
            }
            $request->session()->flash('captcha',$str);
            $sess = session('captcha');
            return $sess;
        }
    }


    //退出登录
    public function login_out(Request $request)
    {
        $request->session()->flush();
        return redirect('login/login_in');

    }


    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
